package com.deltabravomedia.theoldtoad;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class Map extends MapActivity {	
	private GeoPoint oldToad = new GeoPoint(43153263,-77595823);
	private GeoPoint currentLocation = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.map);
	    MapView mapView = (MapView) findViewById(R.id.mapview);
	    mapView.setBuiltInZoomControls(true);
	    
	    List<Overlay> mapOverlays = mapView.getOverlays();
	    Drawable drawable = this.getResources().getDrawable(R.drawable.mapicon);
	    OldToadMapOverlay itemizedoverlay = new OldToadMapOverlay(drawable, this);
	    OverlayItem overlayitem = new OverlayItem(oldToad, "The Old Toad", "277 Alexander Street");
	    itemizedoverlay.addOverlay(overlayitem);
	    mapOverlays.add(itemizedoverlay);

		LocationManager locator = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
	 // Start with fine location.
	    Location l = locator.getLastKnownLocation(LocationManager.GPS_PROVIDER);
	    if (l == null) {
	      // Fall back to coarse location.
	      l = locator.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
	      if (l != null) {
			Drawable drawable2 = this.getResources().getDrawable(R.drawable.youmapicon);
			OldToadMapOverlay itemizedoverlay2 = new OldToadMapOverlay(drawable2, this);
			int lat = (int)(l.getLatitude() * 1000000);
			int lng = (int)(l.getLongitude() * 1000000);
			currentLocation = new GeoPoint(lat, lng);
			OverlayItem overlayitem2 = new OverlayItem(currentLocation, "You", "You are here!");
			itemizedoverlay2.addOverlay(overlayitem2);
			mapOverlays.add(itemizedoverlay2);
	      }
	    }
	    	    	    
	    MapController mapController = mapView.getController();
	    mapController.setCenter(oldToad);
	    mapController.setZoom(14);
	    
        // Capture our button from layout
        Button addButton = (Button)findViewById(R.id.directions_button);
        addButton.setBackgroundColor(Color.YELLOW);
        // Register the onClick listener with the implementation above
        addButton.setOnClickListener(mDirectionsListener);
	}
	
	 // Create an anonymous implementation of OnClickListener
    private OnClickListener mDirectionsListener = new OnClickListener() {
        public void onClick(View v) {
        	int toLat = oldToad.getLatitudeE6();
        	int toLng = oldToad.getLongitudeE6();
        	int fromLat = currentLocation.getLatitudeE6();
        	int fromLng = currentLocation.getLongitudeE6();
        	
        	
        	Intent i = new Intent(Map.this, Directions.class);
            i.putExtra("toLat", toLat);
            i.putExtra("toLng", toLng);
            i.putExtra("fromLat", fromLat);
            i.putExtra("fromLng", fromLng);
        	startActivity(i);
        }
    };

	@Override
	protected boolean isRouteDisplayed() {
	    return false;
	}
}
